import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSetMetaData; 

public class DBActions
{
    private Statement stmt = null;
    private DBConnection connObj = new DBConnection();
    
    public DBActions(DBConnection _connObj)
    {
		this.connObj = _connObj;
    }
    
        
	public void Insert(String sqlStmt)
	{ 
       try 
          { 
	           stmt = (connObj.createConnection()).createStatement(); 
	           stmt.execute(sqlStmt); 
         	   stmt.close();  
     	  } catch (SQLException sqlExcept)  
      		{ 
	           sqlExcept.printStackTrace();       
    		}   
 	}
 	
 	public void Update(String sqlStmt)
	{ 
       try 
          { 
	           stmt = (connObj.createConnection()).createStatement(); 
	           stmt.execute(sqlStmt); 
         	   stmt.close();  
     	  } catch (SQLException sqlExcept)  
      		{ 
	           sqlExcept.printStackTrace();       
    		}   
 	}
 	
 	public void Delete(String sqlStmt)
	{ 
       try 
          { 
	           stmt = (connObj.createConnection()).createStatement(); 
	           stmt.execute(sqlStmt); 
         	   stmt.close();  
     	  } catch (SQLException sqlExcept)  
      		{ 
	           sqlExcept.printStackTrace();       
    		}   
 	}
 	
 	public void Select(String sqlStmt) 
 	{ 
	  try
	    {   
         	stmt = (connObj.createConnection()).createStatement(); 
	        ResultSet results = stmt.executeQuery(sqlStmt);  
	        ResultSetMetaData rsmd = results.getMetaData(); 
	        int numberCols = rsmd.getColumnCount();

            for (int i=1; i<=numberCols; i++) { 
               //print Column Names 
               System.out.print(rsmd.getColumnLabel(i)+"\t\t"); 
             }            
		     System.out.println("\n-------------------------------------------------"); 
		     	
			 // edited by Toni
			 // edited date: 17/05/2009
			 
            while(results.next()) { 
               //int id = results.getInt(1);
              
               /*String xx = results.getString(1);  
               String yy = results.getString(2); 
		   String zz = results.getString(3);
		   String pp = results.getString(4);
		   String qq = results.getString(5);
               //System.out.println(id + "\t\t" + restName + "\t\t" + cityName); */
               System.out.println();
		   		for (int i=1; i<=numberCols; i++) {
		   			System.out.print(results.getString(i).toString()+"	");
		   		}
		   	
               //Display the results
           } //end of Toni's modification.
           
	         results.close();    
		     stmt.close();  
      } catch (SQLException sqlExcept) 
       {
            sqlExcept.printStackTrace(); 
       }   
	}   
	
	//toni added to check ID
	public int searchSQL(String sqlStmt){
		try
	    {   
         	stmt = (connObj.createConnection()).createStatement(); 
	        ResultSet results = stmt.executeQuery(sqlStmt);  
	    	if(!results.next()) //if not found
	    	{	
	    		results.close();    
		    	stmt.close();  
	    		return 0;
	    	}
	    
	    	
	    results.close();    
	    stmt.close();  			    	
	    	
       }catch (SQLException sqlExcept) 
       {
            sqlExcept.printStackTrace(); 
       } 
       	
       
	   return 1; 
	}
}